package test;

import com.mysql.jdbc.Statement;

import java.sql.*;

public class JDBCUtil {

    /**

     * 获取数据库连接的方法

     * @return Connection conn

     * @throws SQLException

     */

    public static Connection getConnection() throws SQLException {

        String url = "jdbc:mysql://localhost:3306/class?useSSL=false";

        String user = "root";

        String password = "root";

        return DriverManager.getConnection(url,user,password);

    }

    /**

     * 释放连接的方法

     * @param conn

     * @throws SQLException

     */

    public static void releaseSourse(Connection conn) throws SQLException {

        if (conn != null) {

            conn.close();

        }

    }

    /**

     * 释放连接的方法

     * @param conn 数据库连接对象

     * @param stmt 执行SQL语句的对象

     * @throws SQLException

     */

    public static void releaseSourse(Connection conn,Statement stmt) throws SQLException {

        if (stmt != null) {

            stmt.close();

        }

        if (conn != null) {

            conn.close();

        }

    }

    /**

     * 释放连接的方法

     * @param conn 数据库连接对象

     * @param stmt 执行SQL语句的对象

     * @param resultSet 执行SQL语句的返回的结果集

     * @throws SQLException

     */

    public static void releaseSourse(Connection conn, Statement stmt, ResultSet resultSet) throws SQLException {

        if (resultSet != null) {

            resultSet.close();

        }

        if (stmt != null) {

            stmt.close();

        }

        if (conn != null) {

            conn.close();

        }

    }

}
